# -*- coding: utf-8 -*-
"""
Created on Mon Aug 13 09:57:03 2018

@author: jiujiujiu
"""

import time
from cs import day
from Tk import genearteMD5
from GetText import get_html_text
from WriteData import writedata

headers = {'Accept': '*/*',
           'Accept-Language': 'zh-CN,zh;q=0.9',
           'Connection': 'keep-alive',
           'Cookie': 'PHPSESSID=9joavu0dom8e1tsf9ptj1ribm1',
           'Host': 'www.1hucj.com',
           'Referer': 'http://www.1hucj.com/lives/',
           'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
           'X-Requested-With': 'XMLHttpRequest'
           }


def get_data(url, headers):
    html = get_html_text(url, headers)
    if not html:
        print('请求失败')
        return None
    html = eval(html)
    datas = html['data']
    dates = [date['time_1'].replace('.', '-') for date in datas]
    return dates


def count_process_one():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while 1:
        url = 'http://www.1hucj.com/portal/lives/getmore.html?page={}&tagtime=20180811'
        url = url.format(a)
        dates = get_data(url, headers)
        if not dates:
            break
        print('----一乎财经获取第{}页----'.format(a))
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '一乎财经'
    t1 = count_process_one()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0], "yesterday": t1[1], "frontday": t1[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    main()
